<?php
/**
 * Created by PhpStorm.
 * User: mr_lee
 * Date: 2017/10/17
 * Time: 上午10:09
 */

namespace app\leadertest\controller;


use Alipay\Alipayobj;
use app\common\controller\AdminBase;
use app\common\model\Agent;
use app\common\model\AgentSys;
use app\common\model\CardSignup;
use app\common\model\ChangeLog;
use app\common\model\Config;
use app\common\model\DictItem;
use app\common\model\Faq;
use app\common\model\LoginUsers;
use app\common\model\Merchant;
use app\common\model\Paylog;
use think\Controller;
use think\Request;
use think\Session;
use thinkcms\auth\model\AuthRoleUser;

class System extends AdminBase
{
    //系统设置
    public function seting()
    {
        $request = request();
        if ($request->isPost()) {
            $info = request()->param();
            $filename = APP_PATH . "extra" . DS . "webconfig.php";
            put_array($info, $filename);
            $this->success('修改成功');
        } else {
            $info = \think\Config::get('webconfig');
            $this->assign('info', $info);
            return $this->fetch();
        }
    }

    //报名查看
    public function signup()
    {
        $CardSignup = new CardSignup();
        $list = $CardSignup->order('id desc')->paginate(15);
        $this->assign('list', $list);
        return $this->fetch();
    }

    //系统用户管理
    public function userlist()
    {
        $LoginUsers = new LoginUsers();
        $map['level'] = array(array('eq', 1001), array('eq', 1002), array('eq', 1010), "or");
        $list = $LoginUsers->where($map)->select();
        $this->assign('list', $list);
        return $this->fetch();
    }

    //系统用户添加
    public function useradd()
    {
        $LoginUsers = new LoginUsers();
        if (Request()->isPost()) {
            $arr = request()->post();
            $arr['password'] = MD5($arr['password']);
            $sav = $LoginUsers->allowField(true)->validate("LoginUsers.add")->save($arr);
            if ($sav) {
                switch ($arr['level']) {
//                    case "1001":
//                        $roleid = 2;
//                        break;
                    case "1002":
                        $roleid = 7;
                        break;
                    case "1010":
                        $roleid = 8;
                        break;
                }
                $sdata['role_id'] = $roleid;
                $sdata['user_id'] = $LoginUsers->id;
                $AuthRoleUser = new AuthRoleUser();
                $AuthRoleUser->save($sdata);
                $this->success("添加成功");
            } else {
                $this->error($LoginUsers->getError());
            }
        } else {
            $AgentSys = new AgentSys();
            $DictItem = new DictItem();
            $map['type_id'] = 2;
            $oemlist = $AgentSys->select();
            $dice = $DictItem->where($map)->select();
            $this->assign('dice', $dice);
            $this->assign('oemlist', $oemlist);
            return $this->fetch();
        }
    }

    //系统用户修改
    public function useredit($id)
    {
        $LoginUsers = new LoginUsers();
        $uid = $id;
        if (Request()->isPost()) {
            $arr = request()->post();

            if ($arr['password']!=="") {
                $arr['password'] = MD5($arr['password']);
            }else{
                unset($arr['password']);
            }
            $sav = $LoginUsers->allowField(true)->validate("LoginUsers.add")->save($arr, ['id' => $uid]);
            if ($sav) {
                switch ($arr['level']) {
//                    case "1001":
//                        $roleid = 2;
//                        break;
                    case "1002":
                        $roleid = 7;
                        break;
                    case "1010":
                        $roleid = 8;
                        break;
                }
                $sdata['role_id'] = $roleid;
                $AuthRoleUser = new AuthRoleUser();
                $AuthRoleUser->save($sdata, ['user_id' => $uid]);
                $this->success("修改成功！", 'system/userlist');
            } else {
                $this->error('修改失败！', 'system/userlist');
            }
        } else {
            $info = $LoginUsers->where('id', $uid)->find();
            $DictItem = new DictItem();
            $AgentSys = new AgentSys();

            $oemlist = $AgentSys->select();

            $map['type_id'] = 2;
            $dice = $DictItem->where($map)->select();
            $this->assign('dice', $dice);
            $this->assign('info', $info);
            $this->assign('oemlist', $oemlist);
            return $this->fetch();
        }
    }

    //系统用户删除
    public function userdel($id)
    {
        if (Request()->isGet()) {
            $arr = request()->get('id');
            $arr['id'] = $id;
            $LoginUsers = new LoginUsers();
            $res = $LoginUsers->where($arr)->delete();
            if ($res == false) {
                $this->error('删除失败！', 'system/userlist');
            } else {
                $this->success('删除成功！', 'system/userlist');
            }
        }
    }


    //系统设置
    public function syssetting()
    {
        if (request()->isAjax()) {
            $data = request()->post();
            $System = new \app\common\model\System();
            $up = $System->allowField(true)->save($data, ['id' => 1]);
            if ($up) {
                $arr = array('code' => 0, 'message' => '修改成功');
            } else {
                $arr = array('code' => 2, 'message' => '修改失败');
            }
            return json($arr);
        } else {
            $System = new \app\common\model\System();
            $sysconfig = $System->find();
            $this->assign('sysconfig', $sysconfig);
            return $this->fetch();
        }
    }

    //更新日志管理
    public function changelog()
    {
        $ChangLog = new ChangeLog();
        $list = $ChangLog->order('id desc')->select();
        $this->assign('list', $list);
        return $this->fetch();
    }

    //更新日志详情
    public function changeinfo()
    {
        $id = request()->param('id');
        $ChangLog = new ChangeLog();
        $info = $ChangLog->where(['id' => $id])->find();
        $this->assign('info', $info);
        return $this->fetch();
    }

    //更新日志添加
    public function changeadd()
    {
        if (Request()->isAjax()) {
            $arr = request()->post();
//            return json($arr);
            $data['title'] = $arr['title'];
            $data['type'] = $arr['type'];
            $data['version_no'] = $arr['version_no'];
            $data['content'] = $arr['content'];
            $data['down_url'] = $arr['down_url'];
            $data['create_time'] = time();
            $ChangLog = new ChangeLog();
            $res = $ChangLog->allowField(true)->save($data);
            if ($res) {
                $arrs = ['code' => 0, 'msg' => '发布成功'];
            } else {
                $arrs = ['code' => 9001, 'msg' => '发布失败'];
            }
            return json($arrs);
        } else {
            return $this->fetch();
        }
    }

    //更新日志修改
    public function changeedit()
    {
        $id = request()->param('id');
        $ChangLog = new ChangeLog();
        if (Request()->isAjax()) {
            $arr = request()->post();
//            return json($arr);
            $data['title'] = $arr['title'];
            $data['type'] = $arr['type'];
            $data['version_no'] = $arr['version_no'];
            $data['content'] = $arr['content'];
            $data['down_url'] = $arr['down_url'];
            $data['update_time'] = time();
            $id = request()->post('id');
            $res = $ChangLog->allowField(true)->save($data, ['id' => $id]);
            $mysql = $ChangLog->getLastSql();
            if ($res) {
                $arrs = ['code' => 0, 'msg' => '修改成功'];
            } else {
                $arrs = ['code' => 9001, 'msg' => '修改失败', 'mysql' => $arr];
            }
            return json($arrs);
        } else {
            $info = $ChangLog->where(['id' => $id])->find();
            $this->assign('info', $info);
            return $this->fetch();
        }
    }

    //更新日志删除
    public function changedel()
    {
        $id = request()->param('id');
        $ChangLog = new ChangeLog();
        $res = $ChangLog->where(['id' => $id])->delete();
        if ($res) {
            $this->success('删除成功', 'System/changelog');
        } else {
            $this->error('删除失败', 'System/changelog');
        }
    }


    //帮助文档列表
    public function helplog()
    {
        $Faq = new Faq();
        $list = $Faq->select();
        $this->assign('list', $list);
        return $this->fetch();
    }

    //帮助文档添加
    public function helpadd()
    {
        $Faq = new Faq();
        if (Request()->isPost()) {
            $arr = request()->post();
            $data['keyword'] = $arr['keyword'];
            $data['info'] = $arr['info'];
            $data['type'] = $arr['type'];
            $data['adduser'] = Session::get('user.nickname');
            $data['addtime'] = time();
            $res = $Faq->allowField(true)->save($data);
            if ($res) {
                $arrs = ['code' => 0, 'msg' => '发布成功'];
            } else {
                $arrs = ['code' => 9001, 'msg' => '发布失败'];
            }
            return json($arrs);

        } else {
            return $this->fetch();
        }
    }

    //帮助文档修改
    public function helpedit()
    {
        $Faq = new Faq();
        if (Request()->isPost()) {
            $arr = request()->post();
            $data['keyword'] = $arr['keyword'];
            $data['info'] = $arr['info'];
            $data['type'] = $arr['type'];
            $data['adduser'] = Session::get('user.nickname');
            $data['addtime'] = time();
            $res = $Faq->allowField(true)->save($data, ['id' => $arr['id']]);
            if ($res) {
                $arrs = ['code' => 0, 'msg' => '发布成功'];
            } else {
                $arrs = ['code' => 9001, 'msg' => '发布失败'];
            }
            return json($arrs);

        } else {
            $id = request()->param('id');
            $info = $Faq->where(['id' => $id])->find();
//            var_dump($info);
            $this->assign('info', $info);
            return $this->fetch();
        }
    }

//系统oem
    public function sysoemlist()
    {
        $AgentSys = new AgentSys();
        $list = $AgentSys->paginate(10);
        $this->assign('list', $list);
        return $this->fetch();
    }

    public function sysoemadd()
    {
        if (Request()->isPost()) {
            $arr = request()->post();
            $AgentSys = new AgentSys();
            $res = $AgentSys->allowField(true)->save($arr);
            if ($res) {
                $this->success('添加成功', url('System/sysoemlist'));
            } else {
                $this->error('添加失败');

            }
        } else {
            return $this->fetch();
        }

    }

    public function sysoemedit()
    {
        if (Request()->isPost()) {
            $arr = request()->post();
            $AgentSys = new AgentSys();
            $res = $AgentSys->allowField(true)->save($arr, $arr['id']);
            if ($res) {
                $this->success('修改成功');
            } else {
                $this->error('修改失败');

            }
        } else {
            $map['id'] = request()->param('id');
            $AgentSys = new AgentSys();
            $info = $AgentSys->where($map)->find();
            $this->assign('info', $info);
            return $this->fetch();
        }
    }

    public function sysoemdel()
    {
        $id = request()->param('id');
        $AgentSys = new AgentSys();
        $res = $AgentSys->where(['id' => $id])->delete();
        if ($res) {
            $this->success('删除成功', url('System/sysoemlist'));
        } else {
            $this->error('删除失败', url('System/sysoemlist'));
        }
    }

    //帮助文档删除
    public function helpdel()
    {
        $id = request()->param('id');
        $Faq = new Faq();
        $res = $Faq->where(['id' => $id])->delete();
        if ($res) {
            $this->success('删除成功', 'System/helplog');
        } else {
            $this->error('删除失败', 'System/helplog');
        }
    }

}